//
//  AuthViewController.m
//  HaierGMS_APP
//
//  Created by 堂堂 on 16/10/8.
//  Copyright © 2016年 堂堂. All rights reserved.
//

#import "AuthViewController.h"
#import "UIColor+Hex.h"
#import <AliyunOSSiOS/OSSService.h>
#import "MBProgressHUD+MJ.h"
#import "UserDefaultUtil.h"
#import "HttpReauestParam.h"
#import "AFHTTPRequestOperationManager.h"
#import "AuthServiceViewController.h"
#import "AuthLocationViewController.h"
#import "UIImage+WebCache.h"
#import "AddCarViewController.h"
#import <MSFSDK/MSFSDK.h>
#import "LoginViewController.h"

#define ScreenHeight [[UIScreen mainScreen] bounds].size.height//获取屏幕高度，兼容性测试
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width//获取屏幕宽度，兼容性测试
@interface AuthViewController ()

@property UIScrollView *contentView1;
@property UIScrollView *contentView2;
@property UIScrollView  *contentView3;

@property NSInteger height_all;

@property NSInteger pos;
@end

@implementation AuthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _auth_cars = [[NSMutableArray alloc]init];

    
    _line1.backgroundColor = [UIColor colorWithHexString:@"#ff7e00"];
    _line2.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    _line3.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    
    
    [self initContentView1];
    [self initContentView2];
    [self initContentView3];
    
    _contentView2.hidden = YES;
    _contentView3.hidden = YES;
    
    _nameField.delegate = self;
    _cardField.delegate = self;
    _workField.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)top_click:(int) tag{
    if (tag == 0) {
        _line1.backgroundColor = [UIColor colorWithHexString:@"#ff7e00"];
        _line2.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
        
        [_top_image1 setImage:[UIImage imageNamed:@"auth_top_pressed1"]];
        [_top_image2 setImage:[UIImage imageNamed:@"auth_top_default2"]];
    }
    
    if (tag == 1) {
        _line1.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
        _line2.backgroundColor = [UIColor colorWithHexString:@"#ff7e00"];
        _line3.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
        
        [_top_image1 setImage:[UIImage imageNamed:@"auth_top_default1"]];
        [_top_image2 setImage:[UIImage imageNamed:@"auth_top_pressed2"]];
        [_top_image3 setImage:[UIImage imageNamed:@"auth_top_default3"]];
    }
    
    if (tag == 2) {
        _line2.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
        _line3.backgroundColor = [UIColor colorWithHexString:@"#ff7e00"];
        
        [_top_image2 setImage:[UIImage imageNamed:@"auth_top_default2"]];
        [_top_image3 setImage:[UIImage imageNamed:@"auth_top_pressed3"]];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)initContentView1{
    int height = 0;
    //标题
    _contentView1 = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, _content.frame.size.height)];
    UILabel *lab1 = [[UILabel alloc]init];
    lab1.frame = CGRectMake(6, 0, ScreenWidth, 35);
    lab1.text = @"请如实输入以下信息:";
    lab1.font = [UIFont systemFontOfSize:12];
    lab1.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [_contentView1 addSubview:lab1];
    height = height + 35;
    
    UIView *line0 = [[UIView alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 1)];
    line0.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [_contentView1 addSubview:line0];
    height = height + 1;
    
    //真实姓名
    UILabel *lab_name = [[UILabel alloc]init];
    lab_name.frame = CGRectMake(6, height, 120, 40);
    lab_name.text = @"真实姓名";
    lab_name.font = [UIFont systemFontOfSize:13];
    [_contentView1 addSubview:lab_name];
    //输入框
    UITextField *field_name = [[UITextField alloc]init];
    _nameField = field_name;
    field_name.frame = CGRectMake(6  + 120, height, ScreenWidth - 120, 40);
    field_name.placeholder = @"请输入真实姓名";
    field_name.font = [UIFont systemFontOfSize:13];
    [_contentView1 addSubview:field_name];
    height = height + 40;
    
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 1)];
    line1.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
     [_contentView1 addSubview:line1];
    height = height + 1;
    //身份证号码
    UILabel *lab_card = [[UILabel alloc]init];
    lab_card.frame = CGRectMake(6, height, 120, 40);
    lab_card.text = @"身份证号码";
    lab_card.font = [UIFont systemFontOfSize:13];
    [_contentView1 addSubview:lab_card];
    //输入框
    UITextField *field_card = [[UITextField alloc]init];
    _cardField = field_card;
    field_card.frame = CGRectMake(6  + 120, height, ScreenWidth - 120, 40);
    field_card.placeholder = @"请输入身份证号码";
    field_card.font = [UIFont systemFontOfSize:13];
    [_contentView1 addSubview:field_card];
    height = height + 40;
    
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 1)];
    line2.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [_contentView1 addSubview:line2];
    height = height + 1;
    
    //照片
    height = height + 20;
    int weight = ScreenWidth / 2 - 60;
    
    UIControl *imageControl1 = [[UIControl alloc]initWithFrame:CGRectMake(30, height, weight, weight * 3 / 4)];
    imageControl1.tag = 1001;
    [imageControl1 addTarget:self action:@selector(callActionSheetFunc:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView1 addSubview:imageControl1];
    UIControl *imageControl2 = [[UIControl alloc]initWithFrame:CGRectMake(weight + 90, height, weight, weight * 3 / 4)];
    imageControl2.tag = 1002;
    [imageControl2 addTarget:self action:@selector(callActionSheetFunc:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView1 addSubview:imageControl2];
    
    UIImageView *user_image1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, height, weight, weight * 3 / 4)];
    [user_image1 setImage:[UIImage imageNamed:@"auth_user_image1"]];
    UIImageView *user_image2 = [[UIImageView alloc]initWithFrame:CGRectMake(weight + 90, height, weight, weight * 3 / 4)];
    [user_image2 setImage:[UIImage imageNamed:@"auth_user_image2"]];
    [_contentView1 addSubview:user_image1];
    [_contentView1 addSubview:user_image2];
    
    height = height + 20 + weight * 3 / 4;

    UIView *line3 = [[UIView alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 1)];
    line3.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [_contentView1 addSubview:line3];
    height = height + 1;
    
    //标题2
    UILabel *lab2 = [[UILabel alloc]init];
    lab2.frame = CGRectMake(6,height, ScreenWidth, 30);
    lab2.text = @"请上传一张本人手持身份证的照片:";
    lab2.font = [UIFont systemFontOfSize:12];
    lab2.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [_contentView1 addSubview:lab2];
    height = height + 30;
    height = height + 20;
    //手持照片
    UIControl *imageControl3 = [[UIControl alloc]initWithFrame:CGRectMake(30 , height, weight, weight * 3 / 4)];
    imageControl3.tag = 1003;
    [imageControl3 addTarget:self action:@selector(callActionSheetFunc:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView1 addSubview:imageControl3];
    
    UIImageView *user_image3 = [[UIImageView alloc]initWithFrame:CGRectMake(30, height, weight, weight * 3 / 4)];
    [user_image3 setImage:[UIImage imageNamed:@"auth_user_image3"]];
    [_contentView1 addSubview:user_image3];
    
    int textHeight = (weight * 3 / 4 - 10) / 4;
    //提示文字
    UILabel *text1 = [[UILabel alloc]init];
    text1.frame = CGRectMake(weight + 70, height + 5, weight, textHeight);
    text1.text = @"1.需本人上传免冠照;";
    text1.font = [UIFont systemFontOfSize:12];
    [_contentView1 addSubview:text1];
    
    UILabel *text2 = [[UILabel alloc]init];
    text2.frame = CGRectMake(weight + 70, height + textHeight + 5, weight, textHeight);
    text2.text = @"2.照片五官清晰可见;";
    text2.font = [UIFont systemFontOfSize:12];
    [_contentView1 addSubview:text2];
    
    UILabel *text3 = [[UILabel alloc]init];
    text3.frame = CGRectMake(weight + 70, height + textHeight * 2 + 5, weight, textHeight);
    text3.text = @"3.照片身份证号码可见;";
    text3.font = [UIFont systemFontOfSize:12];
    [_contentView1 addSubview:text3];
    
    UILabel *text4 = [[UILabel alloc]init];
    text4.frame = CGRectMake(weight + 70, height + textHeight * 3 + 5, weight, textHeight);
    text4.text = @"查看实例照片";
    text4.font = [UIFont systemFontOfSize:12];
    text4.textColor = [UIColor colorWithHexString:@"#ff7e00"];
    [_contentView1 addSubview:text4];
    height = height + 20 + weight * 3 / 4;
    
    UIView *line4 = [[UIView alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 1)];
    line4.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [_contentView1 addSubview:line4];
    height = height + 1;
    height = height + 20;
    //下一步
    UIButton *nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, height, ScreenWidth - 20, 50)];
    nextBtn.tag = 101;
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitle:@"下一步" forState:UIControlStateSelected];
    [nextBtn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#ff7e00"]] forState:UIControlStateNormal];
    nextBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [nextBtn addTarget:self action:@selector(nextAndLast_Click:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView1 addSubview:nextBtn];
    height = height + 50;
    height = height + 5;
    //登录
    UIButton *loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, height, ScreenWidth - 20, 20)];
    [loginBtn setTitleColor:[UIColor colorWithHexString:@"#ff7e00"] forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor colorWithHexString:@"#ff7e00"] forState:UIControlStateSelected];
    [loginBtn setTitle:@"已有账号?立即登录" forState:UIControlStateNormal];
    [loginBtn setTitle:@"已有账号?立即登录" forState:UIControlStateSelected];
    [loginBtn setBackgroundImage:[self createImageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    [loginBtn addTarget:self action:@selector(login_click) forControlEvents:UIControlEventTouchUpInside];
    [_contentView1 addSubview:loginBtn];
    height = height + 120;
    
    _contentView1.frame = CGRectMake(0, 0, ScreenWidth, height+ 30);
    _contentView1.contentSize = _contentView1.frame.size;

    _imageUrl = [[NSArray alloc]initWithObjects:user_image1,user_image2,user_image3, nil];
    [_content addSubview:_contentView1];
}

-(void)initContentView2{
    _contentView2 = [[UIScrollView alloc]initWithFrame:_content.frame];
    
    int height = 30;
    
    UIView *line0 = [[UIView alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 1)];
    line0.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [_contentView2 addSubview:line0];
    height = height + 1;
    
    //服务类型
    UIButton *content_btn1 = [[UIButton alloc] initWithFrame:CGRectMake(0, height, ScreenWidth, 50)];
    content_btn1.tag = 11;
    [content_btn1 setBackgroundImage:[self createImageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
    [content_btn1 setBackgroundImage:[self createImageWithColor:[UIColor lightGrayColor]] forState:UIControlStateSelected];
    [content_btn1 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *type_lab = [[UILabel alloc]initWithFrame:CGRectMake(0, height + 10, 80, 30)];
    type_lab.text = @"    服务类型";
    type_lab.font = [UIFont systemFontOfSize:13];
    UILabel *type_lab2 = [[UILabel alloc]initWithFrame:CGRectMake(80, height + 10, ScreenWidth- 130, 30)];
    type_lab2.text = @"选择服务类型";
    type_lab2.font = [UIFont systemFontOfSize:13];
    type_lab2.tag = 100001;
    type_lab2.textColor = [UIColor colorWithHexString:@"#cccccc"];
    
    UIImageView *rightImg1 = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth - 50, height + 10, 30, 30)];
    UIImage *img = [UIImage imageNamed:@"left_image"];
    rightImg1.image = img;
    
    //rightImg1.backgroundColor = [UIColor blackColor];
    rightImg1.contentMode = UIViewContentModeCenter;
    [_contentView2 addSubview:content_btn1];
    [_contentView2 addSubview:type_lab];
    [_contentView2 addSubview:type_lab2];
    [_contentView2 addSubview:rightImg1];
    
    height = height + 50;
    
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 1)];
    line1.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [_contentView2 addSubview:line1];
    height = height + 1;

    //服务区域
    UIButton *content_btn2 = [[UIButton alloc] initWithFrame:CGRectMake(0, height, ScreenWidth, 50)];
    content_btn2.tag = 12;
    [content_btn2 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [content_btn2 setBackgroundImage:[self createImageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
    [content_btn2 setBackgroundImage:[self createImageWithColor:[UIColor lightGrayColor]] forState:UIControlStateSelected];
    UILabel *location_lab = [[UILabel alloc]initWithFrame:CGRectMake(0, height + 10, 80, 30)];
    location_lab.text = @"    服务区域";
    location_lab.font = [UIFont systemFontOfSize:13];
    UILabel *location_lab2 = [[UILabel alloc]initWithFrame:CGRectMake(80, height + 10, ScreenWidth- 130, 30)];
    location_lab2.text = @"选择服务区域";
    location_lab2.tag = 100002;
    location_lab2.textColor = [UIColor colorWithHexString:@"#cccccc"];
    location_lab2.font = [UIFont systemFontOfSize:13];
    UIImageView *rightImg2 = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth - 50, height + 10, 30, 30)];
    [rightImg2 setImage:[UIImage imageNamed:@"left_image"]];
    rightImg2.contentMode = UIViewContentModeCenter;
    [_contentView2 addSubview:content_btn2];
    [_contentView2 addSubview:location_lab];
    [_contentView2 addSubview:location_lab2];
    [_contentView2 addSubview:rightImg2];
    
    height = height + 50;
    
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 1)];
    line2.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [_contentView2 addSubview:line2];

    //工作年限
    UILabel *work_lab = [[UILabel alloc]initWithFrame:CGRectMake(0, height, 80, 50)];
    
    work_lab.text = @"    工作年限";
    work_lab.font = [UIFont systemFontOfSize:13];
    UITextField *work_lab2 = [[UITextField alloc]initWithFrame:CGRectMake(80, height, ScreenWidth- 130, 50)];
    _workField = work_lab2;
    work_lab2.placeholder = @"工作年限";
    work_lab2.font = [UIFont systemFontOfSize:13];
    work_lab2.textColor = [UIColor colorWithHexString:@"#cccccc"];
    work_lab2.keyboardType = UIKeyboardTypeNumberPad;
    
    [_contentView2 addSubview:work_lab];
    [_contentView2 addSubview:work_lab2];
    
    height = height + 50;
    
    UIView *line3 = [[UIView alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 1)];
    line3.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [_contentView2 addSubview:line3];
    height = height + 20;
    
    
    //上一步
    UIButton *lastBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, height, ScreenWidth - 20, 50)];
    lastBtn.tag = 102;
    [lastBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [lastBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [lastBtn setTitle:@"上一步" forState:UIControlStateNormal];
    [lastBtn setTitle:@"上一步" forState:UIControlStateSelected];
    [lastBtn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#ff7e00"]] forState:UIControlStateNormal];
    lastBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [lastBtn addTarget:self action:@selector(nextAndLast_Click:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView2 addSubview:lastBtn];
    height = height + 50;
    height = height + 15;
    //下一步
    UIButton *nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, height, ScreenWidth - 20, 50)];
    nextBtn.tag = 103;
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitle:@"下一步" forState:UIControlStateSelected];
    [nextBtn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#ff7e00"]] forState:UIControlStateNormal];
    nextBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [nextBtn addTarget:self action:@selector(nextAndLast_Click:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView2 addSubview:nextBtn];
    height = height + 50;
    height = height + 5;
    //登录
    UIButton *loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, height, ScreenWidth - 20, 20)];
    [loginBtn setTitleColor:[UIColor colorWithHexString:@"#ff7e00"] forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor colorWithHexString:@"#ff7e00"] forState:UIControlStateSelected];
    [loginBtn setTitle:@"已有账号?立即登录" forState:UIControlStateNormal];
    [loginBtn setTitle:@"已有账号?立即登录" forState:UIControlStateSelected];
    [loginBtn setBackgroundImage:[self createImageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    [loginBtn addTarget:self action:@selector(login_click) forControlEvents:UIControlEventTouchUpInside];
    [_contentView2 addSubview:loginBtn];
    
    height = height + 30;
    _contentView2.frame = CGRectMake(0, 0, ScreenWidth, height);
    _contentView2.contentSize = _contentView2.frame.size;
    
    [_content addSubview:_contentView2];
}
-(void)initContentView3{
    _contentView3 = [[UIScrollView alloc]init];
    _contentView3.backgroundColor = [UIColor whiteColor];
    int height = 10;
    //添加车辆
    UIControl *add_content = [[UIButton alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 50)];
    add_content.backgroundColor= [UIColor colorWithHexString:@"#f2f2f2"];
    [add_content addTarget:self action:@selector(addCarClick) forControlEvents:UIControlEventTouchUpInside];
    [_contentView3 addSubview:add_content];
    //图片
    UIImageView *add_img = [[UIImageView alloc]initWithFrame:CGRectMake(20, height + 10 , 30, 30)];
    add_img.contentMode = UIViewContentModeCenter;
    add_img.image = [UIImage imageNamed:@"auth_car_add_image"];
    [_contentView3 addSubview:add_img];
    
    UILabel *add_lab = [[UILabel alloc]initWithFrame:CGRectMake(60, height , ScreenWidth - 120, 50)];
    add_lab.font = [UIFont systemFontOfSize:13];
    add_lab.text = @"添加车辆信息";
    [_contentView3 addSubview:add_lab];
    
    UIImageView *right_img = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth - 50, height + 10, 30, 30)];
    right_img.contentMode = UIViewContentModeCenter;
    right_img.image = [UIImage imageNamed:@"left_image"];
    [_contentView3 addSubview:right_img];
    
    height = height + 50;
    height = height + 30;
    //上一步
    UIButton *lastBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, height, ScreenWidth - 20, 50)];
    lastBtn.tag = 104;
    [lastBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [lastBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [lastBtn setTitle:@"上一步" forState:UIControlStateNormal];
    [lastBtn setTitle:@"上一步" forState:UIControlStateSelected];
    [lastBtn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#ff7e00"]] forState:UIControlStateNormal];
    lastBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [lastBtn addTarget:self action:@selector(nextAndLast_Click:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView3 addSubview:lastBtn];
    height = height + 50;
    height = height + 15;
    //下一步
    UIButton *nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, height, ScreenWidth - 20, 50)];
    nextBtn.tag = 105;
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [nextBtn setTitle:@"立即认证" forState:UIControlStateNormal];
    [nextBtn setTitle:@"立即认证" forState:UIControlStateSelected];
    [nextBtn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#ff7e00"]] forState:UIControlStateNormal];
    nextBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [nextBtn addTarget:self action:@selector(nextAndLast_Click:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView3 addSubview:nextBtn];
    height = height + 50;
    height = height + 5;
    //登录
    UIButton *loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, height, ScreenWidth - 20, 20)];
    [loginBtn setTitleColor:[UIColor colorWithHexString:@"#ff7e00"] forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor colorWithHexString:@"#ff7e00"] forState:UIControlStateSelected];
    [loginBtn setTitle:@"已有账号?立即登录" forState:UIControlStateNormal];
    [loginBtn setTitle:@"已有账号?立即登录" forState:UIControlStateSelected];
    [loginBtn setBackgroundImage:[self createImageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    [loginBtn addTarget:self action:@selector(login_click) forControlEvents:UIControlEventTouchUpInside];
    [_contentView3 addSubview:loginBtn];
    
    height = height + 30;
    _height_all = height;
    _contentView3.frame = CGRectMake(0, 0, ScreenWidth, height);
    
    [_content addSubview:_contentView3];
}


-(void)updateContentView3:(int) height{
    
    for ( UIView *view in _contentView3.subviews) {
        view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y + height, view.frame.size.width, view.frame.size.height);
    }
    
}
- (IBAction)back_click:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if([textField isFirstResponder]){
        [textField  resignFirstResponder];
    }
    return YES;
}
- (UIImage*) createImageWithColor: (UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage*theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    return theImage;
}

-(void)addCarClick{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"App_Main" bundle:[NSBundle mainBundle]];
    
    AddCarViewController *vc = [sb instantiateViewControllerWithIdentifier:@"auth_add_car_view"];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
- (IBAction)touchView:(id)sender {
    [self.view endEditing:YES];
}

-(void)nextAndLast_Click:(UIView *)view{
    if (view.tag == 101) {
        //1下一步
        
        _auth_name = _nameField.text;
        _auth_card_no = _cardField.text;
        if ([_auth_name isEqualToString:@""] || [_auth_card_no isEqualToString:@""]) {
            [MBProgressHUD showError:@"请填写个人信息"];
            return;
        }
        if ([_auth_user_image1 isEqualToString:@""] || [_auth_user_image2 isEqualToString:@""] || [_auth_user_image3 isEqualToString:@""]) {
            [MBProgressHUD showError:@"请上传照片"];
            return;
        }
        
        _contentView1.hidden = YES;
        _contentView2.hidden = NO;
        
        [self top_click:1];
        _pos = 1;
    }
    if (view.tag == 102) {
        //2上一步
        _contentView1.hidden = NO;
        _contentView2.hidden = YES;
        
        [self top_click:0];
        _pos = 0;
    }
    
    if (view.tag == 103) {
        
        _auth_work = _workField.text;
        if ([_auth_type isEqualToString:@""] || [_auth_location isEqualToString:@""] || [_auth_work isEqualToString:@""]) {
            [MBProgressHUD showError:@"请填写个人信息"];
            return;
        }
        
        //2下一步
        _contentView2.hidden = YES;
        _contentView3.hidden = NO;
        
        [self top_click:2];
        _pos = 2;
    }
    if (view.tag == 104) {
        //3上一步
        _contentView2.hidden = NO;
        _contentView3.hidden = YES;
        
        [self top_click:1];
        _pos = 1;
    }
    if (view.tag == 105) {
        //3提交
        [self limitData];
    }
}

-(void)onClick:(UIView *)view{
    
    int tag = view.tag;
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"App_Main" bundle:[NSBundle mainBundle]];
    if (tag == 11) {
        AuthServiceViewController *vc = [sb instantiateViewControllerWithIdentifier:@"auth_service_view"];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (tag == 12) {
        AuthLocationViewController *vc = [sb instantiateViewControllerWithIdentifier:@"auth_location_view"];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

/**
 @ 调用ActionSheet
 */
- (void)callActionSheetFunc:(UIView *) controll{
    
    self.add_image_pos = controll.tag;
    
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册选择",  nil];
    }else{
        self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"从相册选择",  nil];
    }
    
    self.actionSheet.tag = 1000;
    [self.actionSheet showInView:self.view];
}

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (actionSheet.tag == 1000) {
        NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 0:
                    //来源:相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:
                    //来源:相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                case 2:
                    return;
            }
        }
        else {
            if (buttonIndex == 2) {
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        // 跳转到相机或相册页面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = sourceType;
        
        [self presentViewController:imagePickerController animated:YES completion:^{
            
        }];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
    //获得的图片
    
    if (_add_image_pos == 1001) {
        UIImageView *image = _imageUrl[0];
        image.image = img;
    }
    if (_add_image_pos == 1002) {
        UIImageView *image = _imageUrl[1];
        image.image = img;
    }
    if (_add_image_pos == 1003) {
        UIImageView *image = _imageUrl[2];
        image.image = img;
    }
    NSData *imgData = UIImageJPEGRepresentation(img, 1.0);
    [self uploadImage:imgData];
    
    
}

-(void)uploadImage:(NSData *)img{
    
    
    
    id<OSSCredentialProvider> credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:OOS_KEY_ID secretKey:OOS_KEY_SCERET];
    
    OSSClient *client = [[OSSClient alloc] initWithEndpoint:@"http://oss-cn-shanghai.aliyuncs.com" credentialProvider:credential];
    
    OSSPutObjectRequest * put = [OSSPutObjectRequest new];
    
    // 必填字段
    put.bucketName = OOS_KEY_BUCKET;
    
    NSString *filePath = [NSString stringWithFormat:@"%@.png",[self getUniqueStrByUUID]];
    put.objectKey = filePath;
    
    NSString *imageUrlPath =[NSString stringWithFormat:@"http://%@.oss-cn-shanghai.aliyuncs.com/%@",OOS_KEY_BUCKET,filePath];
    
    if (_add_image_pos == 1001) {
        _auth_user_image1 = imageUrlPath;
    }
    if (_add_image_pos == 1002) {
         _auth_user_image2 = imageUrlPath;
    }
    if (_add_image_pos == 1003) {
         _auth_user_image3 = imageUrlPath;
    }
    // put.uploadingFileURL = [NSURL fileURLWithPath:@"<filepath>"];
    put.uploadingData = img; // 直接上传NSData
    
    // 可选字段，可不设置
    put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
        // 当前上传段长度、当前已经上传总长度、一共需要上传的总长度
        NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
    };
    
    // 以下可选字段的含义参考： https://docs.aliyun.com/#/pub/oss/api-reference/object&PutObject
    // put.contentType = @"";
    // put.contentMd5 = @"";
    // put.contentEncoding = @"";
    // put.contentDisposition = @"";
    
    // put.objectMeta = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value1", @"x-oss-meta-name1", nil]; // 可以在上传时设置元信息或者其他HTTP头部
    
    [MBProgressHUD showMessage:@"上传图片"];
    OSSTask * putTask = [client putObject:put];
    
    [putTask continueWithBlock:^id(OSSTask *task) {
        
        if (!task.error) {
            dispatch_sync(dispatch_get_main_queue(), ^{
                //Update UI in UI thread here
                NSLog(@"上传图片结果22");
                [MBProgressHUD hideHUD];
                [MBProgressHUD showSuccess:@"上传成功"];
                NSLog(@"upload object success111!");
            });
            
            NSLog(@"upload object success222!");
        } else {
            dispatch_sync(dispatch_get_main_queue(), ^{
                //Update UI in UI thread here
                
                [MBProgressHUD hideHUD];
                [MBProgressHUD showError:@"上传失败"];
                if (_add_image_pos == 1001) {
                    UIImageView *image = _imageUrl[0];
                    image.image = [UIImage imageNamed:@"auth_user_image1"];
                    _auth_user_image1 = @"";
                }
                if (_add_image_pos == 1002) {
                    UIImageView *image = _imageUrl[1];
                    image.image = [UIImage imageNamed:@"auth_user_image2"];
                    _auth_user_image2 = @"";
                }
                if (_add_image_pos == 1003) {
                    UIImageView *image = _imageUrl[2];
                    image.image = [UIImage imageNamed:@"auth_user_image3"];
                    _auth_user_image3 = @"";
                }
            });
        }
        
        
        return nil;
    }];
}
- (NSString *)getUniqueStrByUUID
{
    CFUUIDRef    uuidObj = CFUUIDCreate(nil);//create a new UUID
    
    //get the string representation of the UUID
    
    NSString    *uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(nil, uuidObj);
    
    CFRelease(uuidObj);
    
    return uuidString ;
    
}

-(void)viewDidAppear:(BOOL)animated{
    //刷新返回的数据
    if(_pos == 1){
        if (_auth_type != nil && ![_auth_type isEqualToString:@""] ) {
            UILabel *typeLab = [self.view viewWithTag:100001];
            typeLab.text = _auth_type;
        }
        if (_auth_location != nil && ![_auth_location isEqualToString:@""] ) {
            UILabel *locationLab = [self.view viewWithTag:100002];
            locationLab.text = _auth_location;
        }
    }
    
    if (_pos == 2) {
        if (_auth_cars.count > 0) {
            [self upDateCarView];
            
        }
    }
    [super viewDidAppear:animated];
}


-(int)upDateCarView{
    UIView *view = [_contentView3 viewWithTag:111111];
    int oldHeight = 0;
    if (view != nil) {
        oldHeight = view.frame.size.height;
        [view removeFromSuperview];
    }
    view = [[UIView alloc]init];
    view.tag = 111111;
    
    int height = 10;
    int width = (ScreenWidth - 100) / 3 ;
    for (int i = 0; i < _auth_cars.count; i++) {
        
        NSDictionary *dic = _auth_cars[i];
        //第一张图片
        UIImage_WebCache *one_image = [[UIImage_WebCache alloc]initWithFrame:CGRectMake(20, height, width, width * 3 / 4)];
        one_image.image = [UIImage imageNamed:@"default_image"];
        [one_image loadImage:[NSURL URLWithString:dic[@"photos1"]]];
        //第二张图片
        UIImage_WebCache *two_image = [[UIImage_WebCache alloc]initWithFrame:CGRectMake(width + 60, height, width, width * 3 / 4)];
        two_image.image = [UIImage imageNamed:@"default_image"];
        [two_image loadImage:[NSURL URLWithString:dic[@"photos2"]]];
        
        CGSize size = [dic[@"license"] sizeWithFont:[UIFont systemFontOfSize:14]];
        UILabel *license_lab = [[UILabel alloc]initWithFrame:CGRectMake(80 + width * 2 , height, width,size.height)];
        license_lab.text = dic[@"license"];
        license_lab.font = [UIFont systemFontOfSize:14];
        
        CGSize size2 = [dic[@"type"] sizeWithFont:[UIFont systemFontOfSize:13]];
        UILabel *type_lab = [[UILabel alloc]initWithFrame:CGRectMake(80 + width * 2 , height + size.height, width,size2.height)];
        type_lab.text = dic[@"type"];
        type_lab.font = [UIFont systemFontOfSize:13];
        type_lab.numberOfLines = 0;
        
        height = height + width * 3 / 4;
        height = height + 10;
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        line.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
        height = height + 1;
    
        [view addSubview:one_image];
        [view addSubview:two_image];
        [view addSubview:license_lab];
        [view addSubview:type_lab];
        [view addSubview:line];
    }
    view.frame = CGRectMake(0, 0, ScreenWidth, height);
    
    [self updateContentView3:height - oldHeight];
    _contentView3.frame = CGRectMake(0, 0, ScreenWidth, height + _height_all);
    [_contentView3 addSubview:view];
    _contentView3.contentSize = _contentView3.frame.size;
    
    return height - oldHeight;
}

-(void)limitData{
    
    NSDictionary *dic = @{@"name":_auth_name,@"id_card":_auth_card_no,@"id_photos1":_auth_user_image1,@"id_photos2":_auth_user_image2,@"worker_photos":_auth_user_image3,@"service_type":_auth_type,@"service_area":_auth_location2,@"work_years":_auth_work,@"truck":_auth_cars,@"type":_type,@"identifier":[UserDefaultUtil getUserId]};
    
    HttpReauestParam *param = [[HttpReauestParam alloc]initWithData:@"app_Personal_information_verification" content:[self dictionaryToJson:dic] urlType:0];
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    [MBProgressHUD showMessage:@"提交验证"];
    [manage POST:param.SEVICE_URL parameters:param.param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [MBProgressHUD hideHUD];
        NSDictionary *data = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        if ([data[@"success"] boolValue]) {
            [MBProgressHUD showSuccess:@"提交成功"];
            
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [MBProgressHUD showError:data[@"errorMsg"]];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"网络连接失败!"];
    }];
}

-(NSString*)dictionaryToJson:(id)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}
-(void)login_click{
    UIStoryboard *sb= [UIStoryboard storyboardWithName:@"App_Main" bundle:[NSBundle mainBundle]];
    LoginViewController *loginVc = [sb instantiateViewControllerWithIdentifier:@"login_view"];
    [self presentViewController:loginVc animated:YES completion:nil];
    
}


@end
